package com.cw.base.jeeyc.biz.converter;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cw.base.jeeyc.dao.model.ApiDatabaseDo;
import com.cw.base.common.model.enums.EnumDatabaseType;
import com.cw.base.jeeyc.api.dto.ApiDatabaseDto;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.wrapper.BaseWrapper;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;

/**
 * api数据库信息转换类
 *
 * @author chenw
 * @date 2019-08-24 10:59:29
 */
public class ApiDatabaseConverter {

    /**
     * do转dto
     *
     * @param model
     * @return
     */
    public static ApiDatabaseDto do2Dto(ApiDatabaseDo model) {
        if (model == null) {
            return null;
        }
        return ApiDatabaseDto.builder()
            .id(model.getId())
            .databaseCode(model.getDatabaseCode())
            .databaseName(model.getDatabaseName())
            .databaseDialect(model.getDatabaseDialect())
            .databaseType(EnumDatabaseType.find(model.getDatabaseType()))
            .databaseDriver(model.getDatabaseDriver())
            .databaseUser(model.getDatabaseUser())
            .databasePassword(model.getDatabasePassword())
            .databaseUrl(model.getDatabaseUrl())
            .channelNo(model.getChannelNo())
            .createBy(model.getCreateBy())
            .createDate(model.getCreateDate())
            .updateBy(model.getUpdateBy())
            .updateDate(model.getUpdateDate())
            .build();
    }

    /**
     * dto转do
     *
     * @param dto
     * @return
     */
    public static ApiDatabaseDo dto2Do(ApiDatabaseDto dto) {
        if (dto == null) {
            return null;
        }
        return ApiDatabaseDo.builder()
            .id(dto.getId())
            .databaseCode(dto.getDatabaseCode())
            .databaseName(dto.getDatabaseName())
            .databaseDialect(dto.getDatabaseDialect())
            .databaseType(dto.getDatabaseType()
                .getCode())
            .databaseDriver(dto.getDatabaseDriver())
            .databaseUser(dto.getDatabaseUser())
            .databasePassword(dto.getDatabasePassword())
            .databaseUrl(dto.getDatabaseUrl())
            .channelNo(dto.getChannelNo())
            .createBy(dto.getCreateBy())
            .createDate(dto.getCreateDate())
            .updateBy(dto.getUpdateBy())
            .updateDate(dto.getUpdateDate())
            .build();
    }

    /**
     * doList转dtoList
     *
     * @param doList
     * @return
     */
    public static List<ApiDatabaseDto> doList2DtoList(List<ApiDatabaseDo> doList) {
        if (CollectionUtil.isEmpty(doList)) {
            return null;
        }
        return Lists.newArrayList(Iterators.transform(doList.iterator(), ApiDatabaseConverter::do2Dto));
    }

    /**
     * dtoList转doList
     *
     * @param dtoList
     * @return
     */
    public static List<ApiDatabaseDo> dtoList2DoList(List<ApiDatabaseDto> dtoList) {
        if (CollectionUtil.isEmpty(dtoList)) {
            return null;
        }
        return Lists.newArrayList(Iterators.transform(dtoList.iterator(), ApiDatabaseConverter::dto2Do));
    }

    public static LambdaQueryWrapper buildWrapper(ApiDatabaseDto request) {
        if(request == null ){
            return new LambdaQueryWrapper<ApiDatabaseDo>();
        }
        LambdaQueryWrapper<ApiDatabaseDo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        BaseWrapper<ApiDatabaseDo> baseWrapper = new BaseWrapper<>();
        baseWrapper.buildBaseWrapper(lambdaQueryWrapper, request);
        if (StringUtils.isNotBlank(request.getDatabaseCode())) {
            lambdaQueryWrapper.eq(ApiDatabaseDo::getDatabaseCode, request.getDatabaseCode());
        }
        if(StringUtils.isNotBlank(request.getDatabaseName())){
            lambdaQueryWrapper.likeRight(ApiDatabaseDo::getDatabaseName,request.getDatabaseName());
        }
        if(null != request.getCreateDateStart()){
            lambdaQueryWrapper.ge(ApiDatabaseDo::getCreateDate,request.getCreateDateStart());
        }
        if(null != request.getCreateDateEnd()){
            lambdaQueryWrapper.le(ApiDatabaseDo::getCreateDate,request.getCreateDateEnd());
        }
        return lambdaQueryWrapper;
    }
}
